﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Management.Automation;
using System.Text.RegularExpressions;

namespace CitrixPVSWrapper
{

    class ValidateIpAddress : ValidateArgumentsAttribute
    {
        protected override void Validate(object arguments, EngineIntrinsics engineIntrinsics)
        {
            Regex r = new Regex(RegexDefs.IpAddressRegex, RegexOptions.IgnoreCase);
            if (!r.Match(arguments.ToString()).Success)
            {
                throw new ParameterBindingException("IP address is not in a valid format. IP address should be in the format 0.0.0.0");
            }
        }
    }

    class ValidateMacAddress : ValidateArgumentsAttribute
    {
        protected override void Validate(object arguments, EngineIntrinsics engineIntrinsics)
        {
            Regex r = new Regex(RegexDefs.MacAddressRegex, RegexOptions.IgnoreCase);
            if (!r.Match(arguments.ToString()).Success)
            {
                throw new ParameterBindingException("MAC address is not in a valid format. MAC address should be in the format XX-XX-XX-XX-XX-XX");
            }
        }
    }

    static class Validate
    {
        public static void MutuallyExclusiveParameters(Dictionary<string, object> BoundParameters, string[] ExclusiveParameters, Boolean AtLeastOneRequired = false)
        {
            int HitCount = 0;
            
            foreach(string EP in ExclusiveParameters)
            { if (BoundParameters.ContainsKey(EP)) { HitCount += 1; }; }
            
            if (HitCount > 1) 
            {
                throw new ParameterBindingException("The following parameters are mutually exclusive: " + String.Join(", ", ExclusiveParameters));
            }

            if (AtLeastOneRequired && HitCount == 0)
            {
                throw new ParameterBindingException("One of the following mutually exclusive parameters is required for this Cmdlet: " + String.Join(", ", ExclusiveParameters));
            }
            
        }

        public static void DependentParameters(Dictionary<string, object> BoundParameters, string[] DependentParameters)
        {
            int HitCount = 0;

            foreach (string DP in DependentParameters)
            { if (BoundParameters.ContainsKey(DP)) { HitCount += 1; }; }

            if (HitCount != DependentParameters.Count() && HitCount != 0)
            {
                throw new ParameterBindingException("The following parameters are dependent on each other and must all be specified: " + String.Join(", ", DependentParameters));
            }

        }

        public static void ValidateIpAddressArray(string[] IpAddresses)
        {
            foreach (string Ip in IpAddresses)
            {
                Regex r = new Regex(RegexDefs.IpAddressRegex, RegexOptions.IgnoreCase);
                if (!r.Match(Ip).Success)
                {
                    throw new ParameterBindingException("IP address is not in a valid format. IP address should be in the format 0.0.0.0");
                }
            }
        }
    }
    
}